#include "studentsql.h"

StudentSql::StudentSql()
{
    db = QSqlDatabase::addDatabase("QMYSQL");//添加mysql
}

void StudentSql::ConnectSql(const QString &hostname)
{
    db.setHostName(hostname);
    db.setUserName("root");
    db.setPassword("123456");
    db.setDatabaseName("test268");
    db.setPort(3306);
    if(!db.open())
    {
        qDebug() << db.lastError() <<endl;
        return ;
    }
    qDebug() << "打开成功\n";
}

void StudentSql::SetFakeStuInfo()
{

}

void StudentSql::SetStuId(const QString& StuId)
{
    this->StuId = StuId;
}

void StudentSql::GetInfoFromSql(QStringList &l)
{
    QSqlQuery query(db);

    QString Key = StuId;//主键
    QString table = "StuInfo";//表名

    QString Getname = "select name from " + table + " where id = " + Key;
    QString Getsex = "select sex from " + table + " where id = " + Key;
    QString Getbirthday = "select birthday from " + table + " where id = " + Key;
    QString Getcity = "select city from " + table + " where id = " + Key;
    QString Getresidence = "select residence from " + table + " where id = " + Key;
    QString Getphone = "select phone from " + table + " where id = " + Key;
    QString Getemail = "select email from " + table + " where id = " + Key;

    QStringList list;
    list.push_back(std::move(Getname));
    list.push_back(std::move(Getsex));
    list.push_back(std::move(Getbirthday));
    list.push_back(std::move(Getcity));
    list.push_back(std::move(Getresidence));
    list.push_back(std::move(Getphone));
    list.push_back(std::move(Getemail));

    int i = 0;
    for(auto ite : list)
    {
        if(query.exec(ite))
        {
            query.next();
            l[i] = query.value(0).toString();
        }
        i++;
    }
}

void StudentSql::GetScoreFromSql(std::vector<std::pair<std::string, int> > &v)
{
    QSqlQuery query(db);
    QString GetScore = "select LesId,Score from Score where StuId = " + StuId;

    if(query.exec(GetScore))
    {
        while(query.next())
        {
            QString LesId = query.value("LesId").toString();

            QSqlQuery tmpquery(db);
            LesId = "select name from Lesson where id = " + LesId;
            tmpquery.exec(LesId);
            if(tmpquery.next())
            {
                std::string LesName = tmpquery.value("name").toString().toStdString();
                int score = query.value("Score").toInt();
                std::pair<std::string,int> pair = {LesName,score};
                v.push_back(std::move(pair));
            }

        }
    }
}

void StudentSql::GetCommentFromSql(QString &str)
{
    QSqlQuery query;
    QString GetComment = "select comment from StuInfo where id = " + StuId;

    if(query.exec(GetComment))
        if(query.next())
            str += query.value("comment").toString();
}

void StudentSql::GetStageFromSql(QString &str)
{
    QSqlQuery query(db);
    QString GetStage = "select Stage from StuInfo where id = " + StuId;
    if(query.exec(GetStage))
        if(query.next())
            str = query.value("Stage").toString();
}
